/**
* Copyright (C) 2013-2024 Nanjing Pengyun Network Technology Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/ 

package py.datanode.page;

import org.junit.Assert;
import org.junit.Test;
import py.archive.ArchiveOptions;
import py.archive.ArchiveType;
import py.archive.page.PageAddress;
import py.common.tlsf.bytebuffer.manager.TlsfByteBufferManagerFactory;
import py.datanode.configuration.DataNodeConfiguration;
import py.datanode.page.impl.PageAddressGenerator;
import py.datanode.storage.impl.StorageBuilder;
import py.datanode.test.DataNodeConfigurationForTest;
import py.storage.Storage;
import py.test.TestBase;

public class PageAddressGeneratorTest extends TestBase {
  private static final int FLEXIBLE_LIMIT_IN_ONE_ARCHIVE = 20;
  protected int segmentCount = 10;

  @Test
  public void generatorShadowPagesTest() throws Exception {
    DataNodeConfiguration cfg = new DataNodeConfigurationForTest();
    cfg.setPageSize(8192);
    cfg.setSegmentUnitSize(16 * 1024 * 1024);
    TlsfByteBufferManagerFactory.init(512, 1024 * 1024 * 10, true);
    cfg.setFlexibleCountLimitInOneArchive(FLEXIBLE_LIMIT_IN_ONE_ARCHIVE);
    ArchiveOptions.initContants(cfg.getPageSize(), cfg.getSegmentUnitSize(),
        cfg.getFlexibleCountLimitInOneArchive());
    Assert.assertEquals(ArchiveOptions.SEGMENTUNIT_DESCDATA_LENGTH,
        ArchiveOptions.SEGMENTUNIT_METADATA_LENGTH + ArchiveOptions.SEGMENTUNIT_BITMAP_LENGTH
            + ArchiveOptions.SEGMENTUNIT_ACCEPTOR_LENGTH);
    long storageSize = 0;
    // archive meta data length
    storageSize += (long) ArchiveType.RAW_DISK.getArchiveHeaderLength();
    // all arbiters length
    storageSize += ArchiveOptions.ALL_FLEXIBLE_LENGTH;

    storageSize += segmentCount * ArchiveOptions.BRICK_DESCDATA_LENGTH;

    storageSize = ((storageSize + ArchiveOptions.SEGMENT_UNIT_DATA_ALIGNED_SIZE - 1)
        / ArchiveOptions.SEGMENT_UNIT_DATA_ALIGNED_SIZE)
        * ArchiveOptions.SEGMENT_UNIT_DATA_ALIGNED_SIZE;

    storageSize +=
        ((segmentCount) * (ArchiveOptions.SEGMENT_PHYSICAL_SIZE));

    Storage storage = StorageBuilder.getDummyStorage((int) storageSize);

    long offsetInArchive = 431742976L;
    long offsetSegment = 3923968L;

    PageAddress pageAddress = PageAddressGenerator
        .generateAddressOfShadowPage(offsetInArchive, offsetSegment, storage);
    Assert.assertEquals(0, pageAddress.getOffsetInSegment());
  }
}
